import DAL from './dal.js';
import util from './util.js'

/**
 * 跟A02职务表相关的方法
 */
class A02DAL {
    constructor() {
        this.init();
    };

    init() {
        this.db = new DAL();
        this.util = new util();
    };

    //在js中 数组和json应该是一样的，数组可以当作key是0 1 2 的json
    toCanRead(originalJsonInfoArray) {

        let canReadArray = new Array();
        for (let key in originalJsonInfoArray) {

            let originalJson = originalJsonInfoArray[key];//这个才是真正的json

            let temp = {};
            for (let item in originalJson) {

                if(originalJson[item]==null||originalJson[item]=='null'){
                    originalJson[item]='';
                }

                if (this.util.A02Dictionary[item]) {
                    if (originalJson[item]) {
                        temp[this.util.A02Dictionary[item]] = originalJson[item].toString().trim()
                    } else {
                        temp[this.util.A02Dictionary[item]] = ''
                    }

                }

            }
            canReadArray.push(temp)
        }
        return canReadArray;
    }

    /**
     * @param {Object} orgID
     * 3、通过单位ID 查询单位的在任的干部ID
     * 返回persionID数组 A0000
     */
    // async SelectPersionIDByOrgID(orgID){

    // 	let temp = this.toCanRead(  await this.db.SelectDataFieldArrayFromDB ('A02','DISTINCT A0000',`WHERE A0201B = \'${orgID}\' and A0281=\'true\' ORDER BY A0201B,length(A0225),A0225 `))

    // 	return temp;
    // }


    /**
     * @param {Object} orgID
     * 4.通过单位ID 查询单位（包括下属单位的）的在任的干部ID
     * 注：正常情况下表A01的统计关系所在单位应该可以使用，但实际情况有
     * 个别干部的统计关系并不在本单位
     * 返回A0000
     */
    async SelectPersionIDIncludBranchOrgByOrgID(orgID){
        let sqlString = `SELECT DISTINCT A02.A0000 FROM "A02","A01" WHERE A01.A0163 = '现职人员' and (A01.A0000 = A02.A0000) and A0201B LIKE \'${orgID}%\' and  A0281='true'  ) `
    	return this.toCanRead(await this.db.SelectDataFieldArrayFromDBByCmd(sqlString));
        //return this.toCanRead(await this.db.SelectDataFieldArrayFromDB ('A02','DISTINCT A0000',`WHERE A0201B LIKE \'${orgID}%\' and A0281=\'1\' ORDER BY length(A0201B),A0201B,length(A0225),A0225 `))

    }

}

export default A02DAL;